/*--------begin change by wangwei----------*/
#include <typedefs.h>
#include <error.h>
#include <std_ops.h>
#include <task_pub.h>
#include <tasks.h>
#include <time.h>
#include <diag.h>
#include <run_ui.h>

/*****************************/
/*   LOCAL VARIABLES         */
/*****************************/

pthread_mutex_t hz50_mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t hz50_cond = PTHREAD_COND_INITIALIZER;

/*****************************/
/*   FUNCTIONS               */
/*****************************/

void *hz50_task(void *arg)
{
  mLog("begin hz50_interrupt!\n");
  while(allRun_flag)
  {
    pthread_mutex_lock(&hz50_mut);
    struct timespec timeout;
    clock_gettime(CLOCK_REALTIME,&timeout);
    timeout.tv_nsec += 20000000; //20ms once as 50Hz
    pthread_cond_timedwait(&hz50_cond, &hz50_mut, &timeout);

    RunTask();

    pthread_mutex_unlock(&hz50_mut);
  }
  pthread_exit(NULL);
}

void *sigbuf_task(void *arg)  //used as the SignalBufferTask() 
{
  mLog("begin sigbuf_task!\n");
   while(!POWER_ON_TEST_COMPLETE)
   {
      Task_yield();    /* wait for power on sequence to complete */
   }
   InitBufStatus();

   while (allRun_flag) 
   {
      Task_yield ();   /* Yield cpu to rest of background */
      /* run command processor */
      CmdProc();
      /* keep track of buffer usage */
      CheckBufStatus();
   }
  pthread_exit(NULL);
}
void *miocmd_task(void *arg)  //used as the MIOCommandTask() 
{
  mLog("begin miocmd_task!\n");
   while(!(POWER_ON_TEST_COMPLETE))
   {
      Task_yield();    /* wait for power on sequence to complete */
   }

   while (allRun_flag)                 /* Loop Forever */
   {
//      IQ_Cmds();
      (void)ProcessCmds();
//      CheckAndExecRs232SelfTest();
//      CheckAndExecInstallWkFile(0);
//      CheckAndExecInstallWkFile(1);
//      CheckAndExecInstallWkFile(2);
//      CheckAndExecInstallWkFile(3);
//      CheckAndExecCreateWkFile();
      Task_yield();           /* Yield cpu to rest of background */
   }
  pthread_exit(NULL);
}
void *rs232_task(void *arg)  //change rs232 to socket communication
{
  mLog("begin SocketTask!\n");
  SocketTask();
  mLog("exit SocketTask!\n");
  pthread_exit(NULL);
}
void *gas_pass_task(void *arg)  //used as the MIOCommandTask() 
{
  mLog("begin gas_pass_task!\n");
  GasPassTask();
  mLog("exit gas_pass_task!\n");
  pthread_exit(NULL);
}
void *uart_task(void *arg)  //communication with uart
{
  mLog("begin uart Task!\n");
  uartTask();
  mLog("exit uartTask!\n");
  pthread_exit(NULL);
}
void *TUI_task(void *arg)  //communication with TUI
{
  mLog("begin TUI Task!\n");
  TUITask();
  mLog("exit TUI Task!\n");
  pthread_exit(NULL);
}


TaskID    Task_create(TaskName task_name, TaskSpec *ts, size_t argn, void *argp)
{
  TaskID task_ID = NULL; 
  switch(task_name)
  {
  case RUN_TASK_NAME:
    if (pthread_create(&task_ID,NULL,hz50_task,NULL)!=0)
    { mLog("Create hz50_interrupt error!\n");}
    break;
  case SIG_BUFFER_NAME:
    if (pthread_create(&task_ID,NULL,sigbuf_task,NULL)!=0)
    { mLog("Create sigbuf_task error!\n");}
    break;
  case MIO_CMD_NAME:
    if (pthread_create(&task_ID,NULL,miocmd_task,NULL)!=0)
    { mLog("Create miocmd_task error!\n");}
    break;
  case RS232_CMD_NAME:
    if (pthread_create(&task_ID,NULL,rs232_task,NULL)!=0)
    { mLog("Create rs232_task error!\n");}
    break;
  case GAS_PASS_NAME:
    if (pthread_create(&task_ID,NULL,gas_pass_task,NULL)!=0)
    { mLog("Create gas_pass_task error!\n");}
    break;
  case UART_TASK_NAME:
    if (pthread_create(&task_ID,NULL,uart_task,NULL)!=0)
    { mLog("Create uart_task error!\n");}
    break;
  case TUI_TASK_NAME:
    if (pthread_create(&task_ID,NULL,TUI_task,NULL)!=0)
    { mLog("Create TUI_task error!\n");}
    break;
  default:
    mLog("Task %d is not exist!\n",task_name);
    break;
  }
  return task_ID;
}

/*--------end change----------------------*/
